package me.zhengjie.minbearwcs.dao.stock.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.dao.stock.inf.PalletDao;
import me.zhengjie.minbearwcs.entity.Mapper.stock.PalletMapper;
import me.zhengjie.minbearwcs.entity.PO.wcs.stock.Pallet;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Optional;

@Repository
@RequiredArgsConstructor
public class PalletDaoImpl implements PalletDao {

    private final PalletMapper mapper;

    @Override
    public boolean insert(Pallet pallet) {
        return mapper.insert(pallet) == 1;
    }

    @Override
    public boolean updateBatchCodeAndInboundDate(String palletCode, String batchCode, LocalDateTime inboundDate) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(Pallet::getPalletCode, palletCode)
                .set(Pallet::getBatchCode, batchCode)
                .set(Pallet::getInboundDate, inboundDate)
                .update();
    }

    @Override
    public String selectMaterialCode(String palletCode) {
        Optional<Pallet> optional = new LambdaQueryChainWrapper<>(mapper)
                .eq(Pallet::getPalletCode, palletCode)
                .oneOpt();
        return optional.map(Pallet::getMaterialCode).orElse(null);
    }

    @Override
    public boolean deleteByPalletCode(String palletCode) {
        return mapper.delete(
                new QueryWrapper<Pallet>()
                        .lambda()
                        .eq(Pallet::getPalletCode, palletCode)
        ) == 1;
    }

    @Override
    public Pallet selectByPalletCode(String palletCode) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(Pallet::getPalletCode, palletCode)
                .one();
    }

    @Override
    public boolean updateMaterialNumByPalletCode(String palletCode, Integer materialNum) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(Pallet::getPalletCode, palletCode)
                .set(Pallet::getMaterialNum, materialNum)
                .update();
    }

}
